const state = {
	qiniuDomain: uni.getStorageSync('qiniuDomain')||'https://qiniu.muqiangyun.cn/',
	systemInfo: uni.getStorageSync('systemInfo')||false,
	environment: uni.getStorageSync('environment')||'',
	statusHeight: uni.getStorageSync('statusHeight')||0,
	navHeight: uni.getStorageSync('navHeight')||0,
	tabbarShow: uni.getStorageSync('tabbarShow')||false,
	tabbarIndex: uni.getStorageSync('tabbarIndex')||-1,
	tabbarHeight: uni.getStorageSync('tabbarHeight')||0,
	bottomSafeHeight: uni.getStorageSync('bottomSafeHeight')||0,
	isAndroid: uni.getStorageSync('isAndroid')||false,
	system: uni.getStorageSync('system')||'',
	qnUpToken: uni.getStorageSync('qnUpToken')||'',
	runEnv: uni.getStorageSync('runEnv')||'',
	appid: uni.getStorageSync('appid')||'',
};

const mutations = {
	SET_APPID: (state, appid) => {
		state.appid = appid;
		uni.setStorageSync('appid', appid)
	},
	SET_RUN_ENV: (state, runEnv) => {
		state.runEnv = runEnv;
		uni.setStorageSync('runEnv', runEnv)
	},
	SET_TABBAR_SHOW: (state, tabbarShow) => {
		state.tabbarShow = tabbarShow;
		uni.setStorageSync('tabbarShow', tabbarShow)
	},
	SET_TABBAR_INDEX: (state, tabbarIndex) => {
		state.tabbarIndex = tabbarIndex;
		uni.setStorageSync('tabbarIndex', tabbarIndex)
	},
	SET_SYSTEM_INFO: (state, systemInfo) => {
		state.systemInfo = systemInfo;
		uni.setStorageSync('systemInfo', systemInfo)
	},
	SET_TABBAR_HEIGHT: (state, tabbarHeight) => {
		state.tabbarHeight = tabbarHeight;
		uni.setStorageSync('tabbarHeight', tabbarHeight)
	},
	SET_IS_ANDROID: (state, isAndroid) => {
		state.isAndroid = isAndroid;
		uni.setStorageSync('isAndroid', isAndroid)
	},
	SET_NAV_SIZE: (state) => {
		const sysInfo = state.systemInfo;
		const system = sysInfo.system;
		const isiOS = system.indexOf('iOS') > -1;
		const navHeight = isiOS ? 44 : 48;
		const safeArea = sysInfo.safeArea;
		const screenHeight = Math.max(sysInfo.screenHeight, sysInfo.screenWidth);
		const safeAreaHeight = Math.max(safeArea.height, safeArea.width);
		const statusBarHeight = sysInfo.statusBarHeight;
		let bottomSafeHeight = 0;
		if (safeArea && safeAreaHeight && screenHeight) {
			bottomSafeHeight = screenHeight - safeAreaHeight - statusBarHeight - 10;
			if (bottomSafeHeight < 0) {
				bottomSafeHeight = 10;
			}
		}
		state.bottomSafeHeight = bottomSafeHeight;
		state.statusHeight = statusBarHeight;
		state.navHeight = navHeight;
		state.system = system;
		uni.setStorageSync('bottomSafeHeight', bottomSafeHeight)
		uni.setStorageSync('statusHeight', statusBarHeight)
		uni.setStorageSync('navHeight', navHeight)
		uni.setStorageSync('system', system)
	},
	SET_QN_UP_TOKEN: (state, qnUpToken) => {
		state.qnUpToken = qnUpToken;
		uni.setStorageSync('qnUpToken', qnUpToken)
	}
};

const actions = {
	$uploadFile: async function({ commit, state }, { filePath, formData }) {
		return new Promise((resolve, reject) => {
			const uploadTask = wx.uploadFile({
				url: 'https://up-z2.qiniup.com',
				filePath,
				name: 'file',
				formData: formData,
				headers: {
					Authorization: `Bearer ${state.qnUpToken}` // 如果有必要的话，可以添加授权头部
				},
				success: function(res) {
					if (res.statusCode === 200) {
						resolve(res);
					} else {
						console.log(res);
						reject(res);
					}
				},
				fail: function(error) {
					reject(error);
					console.error(error);
				}
			});
			
			uploadTask.onProgressUpdate((res) => {
				// 可选：可以显示上传进度
				console.log('上传进度', res.progress);
				console.log('已经上传的数据长度', res.totalBytesSent);
				console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend);
			});
		});
	}
};

const getters = {};

export default {
	namespaced: true, // 启用命名空间
	state,
	mutations,
	actions,
	getters
};